package com.koomii.common.web;

import com.google.gson.JsonSyntaxException;
import com.koomii.common.util.JsonUtil;
import com.koomii.common.util.StringEscapeUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Date;

/**
 * 
 * 商家登录实体 User:
 * 
 * <p>
 * port from LoginContext, remove dependency with
 * struts, replace ActionContext with ThreadLocal.
 * 
 * @see //com.migr.common.context.LoginContext
 * @author
 * 
 */
public class VendorLoginContext {
    private final static ThreadLocal<VendorLoginContext> holder = new ThreadLocal<VendorLoginContext>();

    private final static Log log = LogFactory.getLog(VendorLoginContext.class);
    /***
     * 用户登录会话ID
     */
    private String sessionId;
    /**
     * 用户ID
     */
    private long userId;
    /***
     * 用户所属商家
     */
    private Long vendorId;

    /***
     * 商家编码
     */
    private String vendorCode;
    /***
     * 商家名称
     */
    private String vendorName;
    /**
     * 用户所属组织机构
     */
    private String orgCode;
    /***
     * 部门编号
     */
    private String depCode;
    /***
     * 部门名称
     */
    private String depName;
    /**
     * 用户状态 0正常 1冻结
     */
    private int userStatus;
    /**
     * 登录名称
     */
    private String pin;

    /**
     * 显示名称
     */
    private String nick;
    /**
     * passport cookie的checksum
     */
    private int checksum;
    /**
     * 创建时间 默认为当前时间
     */
    private long created = System.currentTimeMillis();
    /**
     * 过期时间 如果没有指定，就使用拦截器默认的
     */
    private long expires;

    /**
     * 是否持久化
     */
    private boolean persistent;
    /***
     * 所属门店编码
     */
    private String storeCode;

    private Integer vendorType; // 商家类型：0普通商家 1独立商家
    private Long srcVendorId; // 来源商家id
    private String srcOrganCode; // 来源机构编码
    private String srcStoreCode; // 来源门店编码
    
    private String ip;//用户的ip

    public Integer getVendorType() {
        return vendorType;
    }

    public void setVendorType(Integer vendorType) {
        this.vendorType = vendorType;
    }

    public Long getSrcVendorId() {
        return srcVendorId;
    }

    public void setSrcVendorId(Long srcVendorId) {
        this.srcVendorId = srcVendorId;
    }

    public String getSrcOrganCode() {
        return srcOrganCode;
    }

    public void setSrcOrganCode(String srcOrganCode) {
        this.srcOrganCode = srcOrganCode;
    }

    public String getSrcStoreCode() {
        return srcStoreCode;
    }

    public void setSrcStoreCode(String srcStoreCode) {
        this.srcStoreCode = srcStoreCode;
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public String getVendorCode() {
        return vendorCode;
    }

    public void setVendorCode(String vendorCode) {
        this.vendorCode = vendorCode;
    }

    public String getDepName() {
        return depName;
    }

    public void setDepName(String depName) {
        this.depName = depName;
    }

    /**
     * 0 总部用户  1 区域用户  2门店用户
     */
    public int getFlag(){
        if(StringUtils.isEmpty(orgCode) && StringUtils.isEmpty(storeCode)){
            //区域编码为空  总部用户
            return 0;
        }
        if(StringUtils.isNotEmpty(storeCode)){
            //分店编码不为空  门店用户
            return 2;
        }
        return 1;
    }

    public String getVendorName() {
        return vendorName;
    }

    public void setVendorName(String vendorName) {
        this.vendorName = vendorName;
    }

    public String getStoreCode() {
        return storeCode;
    }

    public void setStoreCode(String storeCode) {
        this.storeCode = storeCode;
    }

    public void setVendorId(Long vendorId) {
        this.vendorId = vendorId;
    }

    public void setOrgCode(String orgCode) {
        this.orgCode = orgCode;
    }

    public Long getVendorId() {
        return vendorId;
    }

    public String getOrgCode() {
        return orgCode;
    }

    public String getDepCode() {
        return depCode;
    }

    public void setDepCode(String depCode) {
        this.depCode = depCode;
    }

    public int getUserStatus() {
        return userStatus;
    }

    public void setUserStatus(int userStatus) {
        this.userStatus = userStatus;
    }

    /**
     * Method getUserId returns the userId of this LoginContext object.
     * <p/>
     * 用户ID
     * 
     * @return the userId (type long) of this LoginContext object.
     */
    public long getUserId() {
        return userId;
    }

    /**
     * Method setUserId sets the userId of this LoginContext object.
     * <p/>
     * 用户ID
     * 
     * @param userId
     *            the userId of this LoginContext object.
     */
    public void setUserId(long userId) {
        this.userId = userId;
    }

    /**
     * Method getPin returns the pin of this LoginContext object.
     * <p/>
     * 登录名称
     * 
     * @return the pin (type String) of this LoginContext object.
     */
    public String getPin() {
        return pin;
    }

    /**
     * Method setPin sets the pin of this LoginContext object.
     * <p/>
     * 登录名称
     * 
     * @param pin
     *            the pin of this LoginContext object.
     */
    public void setPin(String pin) {
        this.pin = pin;
    }

    /**
     * Method getNick returns the nick of this LoginContext object.
     * <p/>
     * 显示名称
     * 
     * @return the nick (type String) of this LoginContext object.
     */
    public String getNick() {
        return nick;
    }

    /**
     * Method setNick sets the nick of this LoginContext object.
     * <p/>
     * 显示名称
     * 
     * @param nick
     *            the nick of this LoginContext object.
     */
    public void setNick(String nick) {
        this.nick = nick;
    }

    /**
     * 实际上是将loginContext放到了actionContext中
     * 
     * @param loginContext
     *            对象
     */
    public static void setVendorLoginContext(VendorLoginContext loginContext) {
        holder.set(loginContext);
    }

    /**
     * 取出登录的上下文
     * 
     * @return null 如果没有的话
     */
    public static VendorLoginContext getVendorLoginContext() {
        return holder.get();
    }

    /**
     * 删除上下文、其实一般不用删除
     */
    public static void remove() {
        holder.remove();
    }

    /**
     * 反向构造上下文。
     * 
     * @param value
     * @return 上下文
     * @see #toCookieValue()
     */
    public static VendorLoginContext parse(String value) {
        VendorLoginContext context = null;
        try {
            log.debug("get cookies value="+value);
            context = JsonUtil.g.fromJson(value,VendorLoginContext.class);
        } catch (JsonSyntaxException e) {
            log.error("syntax cookie Json to object VendorLoginContext exception,value="+value,e);
        }
        return context;
    }

    /**
     *
     * 
     * @return json
     * @see #parse(String)
     */
    public String toCookieValue() {
        String cookie = JsonUtil.g.toJson(this);
        log.debug("write cookies value="+cookie);
        return cookie;
    }

    /**
     * 判断是否登录。标准：trim(pin).length > 0
     * 
     * @return true 已经登录 false 没有登录
     */
    public boolean getLogin() {
        return isLogin();
    }

    /**
     * 判断是否登录。标准：trim(pin).length > 0
     * 
     * @return true 已经登录 false 没有登录
     */
    public boolean isLogin() {
        return StringUtils.isNotBlank(pin);
    }

    /**
     * Method setChecksum sets the checksum of this LoginContext object.
     * <p/>
     * passport cookie的checksum
     * 
     * @param checksum
     *            the checksum of this LoginContext object.
     */
    public void setChecksum(int checksum) {
        this.checksum = checksum;
    }

    /**
     * Method getChecksum returns the checksum of this LoginContext object.
     * <p/>
     * passport cookie的checksum
     * 
     * @return the checksum (type int) of this LoginContext object.
     */
    public int getChecksum() {
        return checksum;
    }

    /**
     * Method getCreated returns the created of this LoginContext object.
     * <p/>
     * 创建时间
     * 
     * @return the created (type long) of this LoginContext object.
     */
    public long getCreated() {
        return created;
    }

    /**
     * 创建日期
     * 
     * @return 创建日期
     */
    public Date getCreatedDate() {
        return new Date(created);
    }

    /**
     * Method setCreated sets the created of this LoginContext object.
     * <p/>
     * 创建时间
     * 
     * @param created
     *            the created of this LoginContext object.
     */
    public void setCreated(long created) {
        this.created = created;
    }

    /**
     * Method setCreatedDate sets the createdDate of this LoginContext object.
     * 
     * @param created
     *            the createdDate of this LoginContext object.
     */
    public void setCreatedDate(Date created) {
        this.created = created.getTime();
    }

    /**
     * 设置创建时间等于当前日期
     */
    public void setCreated() {
        this.created = System.currentTimeMillis();
    }

    /**
     * Method getExpires returns the expires of this LoginContext object.
     * <p/>
     * 过期时间
     * 
     * @return the expires (type long) of this LoginContext object.
     */
    public long getExpires() {
        return expires;
    }

    /**
     * Method getExpiresDate returns the expiresDate of this LoginContext
     * object.
     * 
     * @return the expiresDate (type Date) of this LoginContext object.
     */
    public Date getExpiresDate() {
        return new Date(expires);
    }

    /**
     * Method setExpires sets the expires of this LoginContext object.
     * <p/>
     * 过期时间
     * 
     * @param expires
     *            the expires of this LoginContext object.
     */
    public void setExpires(long expires) {
        this.expires = expires;
    }

    /**
     * Method setExpiresDate sets the expiresDate of this LoginContext object.
     * 
     * @param expires
     *            the expiresDate of this LoginContext object.
     */
    public void setExpiresDate(Date expires) {
        this.expires = expires.getTime();
    }

    /**
     * 设置cookie的过期时间，单位：毫秒
     * 
     * @param timeout
     */
    public void setTimeout(long timeout) {
        this.expires = this.created + timeout;
    }

    /**
     * Method isPersistent returns the persistent of this LoginContext object.
     * <p/>
     * 是否持久化
     * 
     * @return the persistent (type boolean) of this LoginContext object.
     */
    public boolean isPersistent() {
        return persistent;
    }

    /**
     * Method setPersistent sets the persistent of this LoginContext object.
     * <p/>
     * 是否持久化
     * 
     * @param persistent
     *            the persistent of this LoginContext object.
     */
    public void setPersistent(boolean persistent) {
        this.persistent = persistent;
    }

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}
}